package net.xumf.dao.master;

import net.xumf.domain.master.SystemPermission;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface SystemPermissionRepository extends PagingAndSortingRepository<SystemPermission, Integer> {

    List<SystemPermission> findByPermissionIdIn(List<Integer> permissionIds);

    @Query(value = "SELECT sp FROM SystemPermission sp WHERE sp.moduleId = ?1",
            countQuery = "SELECT count(sp) FROM SystemPermission sp WHERE sp.moduleId = ?1")
    Page<SystemPermission> findByModuleId(Integer parentId, Pageable pageable);

    /**
     * 查询权限并且查询权限对应模块的path
     *
     * @return {@link SystemPermission}
     */
    @EntityGraph(value = "SystemPermission.module", type = EntityGraph.EntityGraphType.LOAD)
    List<SystemPermission> findAll();

    @Transactional
    @Modifying
    void deleteByModuleIdIn(List<Integer> moduleIds);
}